#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QNetworkReply>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowTitle("客户端");
    manger=new QNetworkAccessManager(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_pushButton_clicked()
{
    //获取输入框url
    QUrl url(ui->lineEdit->text());
    //构造HTTP请求对象
    QNetworkRequest request(url);
    //发送请求
    QNetworkReply* response=manger->get(request);
    //连接槽函数,处理响应
    connect(response,&QNetworkReply::finished,this,[=](){
        //如果正确获取到了
        if(response->error()==QNetworkReply::NoError)
        {
            QString html=response->readAll();
            ui->plainTextEdit->setPlainText(html);
        }
        else ui->plainTextEdit->setPlainText(response->errorString());

        //释放response
        response->deleteLater();
    });
}
